<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script>
			function flatten(arr, result) {
				if (!result) {
					result = [];
				}

				for (var i = 0; i < arr.length; i++) {
					var ele = arr[i];

					if (Object.prototype.toString.call(ele) == '[object Array]') {
						result = (flatten(ele, result));
					} else {
						result.push(ele);
					}
				}

				return result;
			}

			var arr = [1, 2, [3, 4, 5, [6, 7], 8], 9, 10, [11, [12, 13]]];
			console.log(JSON.stringify(flatten(arr)));

			arr = [1, 2, 3, [4, 5],
				[6, [7, [8]]]
			]
			console.log(JSON.stringify(flatten(arr)));
		</script>
	</body>
</html>
